import sqlite3
import logging

def init_db():
    conn = sqlite3.connect('database.db')
    cursor = conn.cursor()
    
    # 创建服务端配置表
    cursor.execute('''
    CREATE TABLE IF NOT EXISTS frp_server_config (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        server_name TEXT NOT NULL,
        server_addr TEXT NOT NULL,
        server_port INTEGER NOT NULL,
        token TEXT NOT NULL
    )
    ''')
    
    # 创建客户端配置表
    cursor.execute('''
    CREATE TABLE IF NOT EXISTS frp_client_config (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL,
        type TEXT NOT NULL,
        local_ip TEXT NOT NULL,
        local_port INTEGER NOT NULL,
        remote_port INTEGER NOT NULL
    )
    ''')
    
    conn.commit()
    conn.close()

def get_db_connection():
    conn = sqlite3.connect('database.db')
    conn.row_factory = sqlite3.Row
    return conn

def add_test_data():
    conn = get_db_connection()
    cursor = conn.cursor()
    
    # 添加服务端配置测试数据
    server_configs = [
        ('阿里云服务器1', '39.104.71.213', 7000, 'adminabc123'),
    ]
    cursor.executemany('''
    INSERT INTO frp_server_config (server_name, server_addr, server_port, token)
    VALUES (?, ?, ?, ?)
    ''', server_configs)
    
    # 添加客户端配置测试数据
    client_configs = [
        ('汽车维修管理-TCP', 'tcp', '127.0.0.1', 5000, 8002),
        ('网站管理-TCP', 'tcp', '127.0.0.1', 80, 8080),
        ('数据库-TCP', 'tcp', '127.0.0.1', 3306, 33060),
        ('远程桌面-TCP', 'tcp', '127.0.0.1', 3389, 33890)
    ]
    cursor.executemany('''
    INSERT INTO frp_client_config (name, type, local_ip, local_port, remote_port)
    VALUES (?, ?, ?, ?, ?)
    ''', client_configs)
    
    conn.commit()
    conn.close()
